package com.haruhi.bot.job.thread;

import com.alibaba.fastjson.JSONObject;
import com.haruhi.bot.entity.bot.PixPicture;
import com.haruhi.bot.entity.haruhi.Wallpaper;
import com.haruhi.bot.service.PixPictureService;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 从haruhi导航数据的壁纸表获取壁纸任务
 */
@Slf4j
public class PutPicFromHaruhiThread implements Runnable{

    private PixPictureService service;

    public PutPicFromHaruhiThread(PixPictureService pixPictureService){
        this.service = pixPictureService;
    }
    @Override
    public void run() {
        List<Wallpaper> haruhiWallpaper = service.getHaruhiWallpaper();
        if(haruhiWallpaper != null && haruhiWallpaper.size() > 0){
            for (int i = 0; i < haruhiWallpaper.size(); i++) {

                Integer integer = service.countPix(haruhiWallpaper.get(i).getMd5());
                if(integer != null && integer >= 1){
                    continue;
                }
                PixPicture pixPicture = new PixPicture();
                pixPicture.setPid(i);
                pixPicture.setView(1);
                pixPicture.setIsR18(false);
                pixPicture.setUid(1);
                pixPicture.setTitle("title-"+i);
                pixPicture.setAuthor("author-"+i);
                pixPicture.setHeight(haruhiWallpaper.get(i).getHeight());
                pixPicture.setWidth(haruhiWallpaper.get(i).getWidth());
                pixPicture.setBookmarks(i);
                pixPicture.setImgP(haruhiWallpaper.get(i).getMd5());
                pixPicture.setImgUrl("http://120.24.224.57:8080"+ haruhiWallpaper.get(i).getUrl());
                if (haruhiWallpaper.get(i).getEnabled()){
                    pixPicture.setTags("美图");
                }else{
                    pixPicture.setTags("色图");
                }
                try {
                    service.insertBotPix(pixPicture);
                }catch (Exception e){
                    System.out.println("发生异常："+e.getMessage()+",参数："+ JSONObject.toJSONString(pixPicture));
                    continue;
                }

            }
        }
        log.info("从haruhi数据库获取图片任务结束");

    }

    public static void execute(PixPictureService pixPictureService){
        new Thread(new PutPicFromHaruhiThread(pixPictureService)).start();
    }

}
